package com.jiulong.service.Impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.jiulong.dao.TFavoriteMapper;
import com.jiulong.model.TAccount;
import com.jiulong.model.TAccountExample;
import com.jiulong.model.TFavorite;
import com.jiulong.model.TFavoriteExample;
import com.jiulong.model.TGift;
import com.jiulong.model.TOrder;
import com.jiulong.model.TOrderExample;
import com.jiulong.model.TUser;
import com.jiulong.model.TUserExample;
import com.jiulong.service.AccountService;
@Service
public class AccountServiceImpl implements AccountService{
	@Autowired
	com.jiulong.dao.TAccountMapper tAccountMapper;
	@Autowired
	com.jiulong.dao.TUserMapper TUserMapper;
	@Autowired
	com.jiulong.dao.TOrderMapper TOrderMapper;
	@Autowired
	com.jiulong.dao.TFavoriteMapper TFavoriteMapper;
	@Override
	public TAccount findAccountbyId(String openid) {
	
		TAccountExample example=new TAccountExample();
		example.createCriteria().andOpenidEqualTo(openid);
		
		List<TAccount> list=tAccountMapper.selectByExample(example);
		System.out.println(list.get(0).getAccount());
		return list.get(0);		
	}
	@Override
	public TAccount findAccountbyNickname(String nickname) {
	
		TAccountExample example=new TAccountExample();
		example.createCriteria().andNicknameEqualTo(nickname);
		
		List<TAccount> list=tAccountMapper.selectByExample(example);
		if(list.toString()=="[]"){
			return null;
		}
		return list.get(0);		
	}
	
	
	@Override
	public TAccount findAccountbyAname(String account) {
		TAccountExample example=new TAccountExample();
		example.createCriteria().andAccountEqualTo(account);	
		List<TAccount> list=tAccountMapper.selectByExample(example);
		if(list.toString()=="[]"){
			return null;
		}
		return list.get(0);		
	}
	//通过手机号判断用户有没有重复注册
	@Override
	public TAccount findAccountbyTell(String tel) {
		TAccountExample example=new TAccountExample();
		example.createCriteria().andAccountEqualTo(tel);	
		List<TAccount> list=tAccountMapper.selectByExample(example);
		if(list.toString()=="[]"){
			return null;
		}
		return list.get(0);		
	}
	
	@Override
	public TOrder findOrderbyId(String account) {
		
		TOrderExample example=new TOrderExample();
		example.createCriteria().andAccountEqualTo(account);
		List<TOrder>list= TOrderMapper.selectByExample(example);
		System.out.println("我的订单回显到前端页面的价格"+list.get(0).getAmount());
		System.out.println(list.get(0).getAccount());
		
		return list.get(0);	
	}
//通过订单号查询出订单状态
	@Override
	public TOrder findOrderbytrade(String out_trade_no) {
		TOrderExample example=new TOrderExample();
		example.createCriteria().andRemarkEqualTo(out_trade_no);
		List<TOrder>list= TOrderMapper.selectByExample(example);
		System.out.println("微信返回的价格"+list.get(0).getAmount());
		System.out.println(list.get(0).getAccount());
		return list.get(0);	
	}

//通过订单号查询出核销订单号

	@Override
	public List findFavoritebytrade(String out_trade_no) {
		TFavoriteExample example=new TFavoriteExample();
		//example.createCriteria().andIdEqualTo(1);
		System.out.println("打印出更改后的out_trade_no"+out_trade_no);
		example.createCriteria().andAccountEqualTo(out_trade_no);
		List<TFavorite>list= TFavoriteMapper.selectByExample(example);
		System.out.println("判断list有没有找到："+list);
		if(list.toString()=="[]"){
			return list;
		}
		System.out.println("核销的优惠券号码"+list.get(0).getProductid());
		return list;	
	}

	@Override
	public TAccount findAccountbyData(String checkData, String pwd) {
		TAccountExample example=new TAccountExample();
		List<TAccount> list=null;
		try{
		example.createCriteria().andPasswordEqualTo(pwd).andAccountEqualTo(checkData);
			list=tAccountMapper.selectByExample(example);
			if(list.toString().equals("[]")){
				example.clear();
				example.createCriteria().andPasswordEqualTo(pwd).andTelEqualTo(checkData);
				list=tAccountMapper.selectByExample(example);
				if(list.toString().equals("[]")){
					return null;
				}
			}
		}catch(Exception e){
			e.printStackTrace();
		}
		return list.get(0);		
	}
//pc端修改用户的信息
	@Override
	public TAccount findAccountbynickName(String nickname, String passwordKey) {
		TAccountExample example=new TAccountExample();
		List<TAccount> list=null;
	try{
		if(passwordKey==null){
			example.createCriteria().andNicknameEqualTo(nickname);
		}else
		{example.createCriteria().andPasswordEqualTo(passwordKey).andNicknameEqualTo(nickname);}
		list=tAccountMapper.selectByExample(example);
		if(list.toString().equals("[]")){
			return null;
		}
	}catch(Exception e){
		e.printStackTrace();
	}
		return list.get(0);
	}
	@Override
	//查找user是否具有审核权限
	public TUser findAccountByWaitCheckd(String email, String username) {
		TUserExample example=new TUserExample();
		List<TUser> list=null;
	try{
		example.createCriteria().andEmailEqualTo(email).andUsernameEqualTo(username);
			list=TUserMapper.selectByExample(example);
			if(list.toString().equals("[]")){
				return null;
			}
	}catch(Exception e){
		e.printStackTrace();
	}
		return list.get(0);
	}
}
